package com.zr.sparesubclass.controller;

import com.zr.sparesubclass.model.*;
import com.zr.sparesubclass.service.SpareSubService;
import com.zr.util.AllRecords;
import com.zr.util.ResultVO;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/12/29.
 */
@RestController
public class SpareSubController {

    @Autowired
    private SpareSubService spareSubService;

    @CrossOrigin
    @PostMapping("metadata/getPullDownList")
    public ResultVO<List<SubClassStatus>> getPullDownList(){
        List<SubClassStatus> subClassStatusList = new ArrayList<>();

        SubClassStatus subClassStatus = new SubClassStatus();
        subClassStatus.setLabel("启用");
        subClassStatus.setValue(true);
        subClassStatusList.add(subClassStatus);

        SubClassStatus subClassStatus1 = new SubClassStatus();
        subClassStatus1.setLabel("禁用");
        subClassStatus1.setValue(false);
        subClassStatusList.add(subClassStatus1);

        return ResultVOBuilder.success(subClassStatusList);
    }

    @CrossOrigin
    @PostMapping("subClass/queryPage")
    public ResultVO<AllRecords> queryPage(@RequestBody SpareSubClass spareSubClass){

        return spareSubService.queryPage(spareSubClass);
    }

    @CrossOrigin
    @PostMapping("subClass/add")
    public ResultVO<SpareSubClassAddVo> spareSubClassAdd(@RequestBody @Valid SpareSubClassAddVo spareSubClassAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return spareSubService.spareSubClassAdd(spareSubClassAddVo);
    }





    @CrossOrigin
    @PostMapping("subClass/update")
    public ResultVO<SpareSubClassUpdateVo> spareSubClassupdate(@RequestBody @Valid SpareSubClassUpdateVo spareSubClassUpdateVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return spareSubService.spareSubClassupdate(spareSubClassUpdateVo);
    }

    @CrossOrigin
    @PostMapping("subClass/updateStatus")
    public ResultVO<SpareSubClassUpdateStatusVo> updateStatus(@RequestBody @Valid SpareSubClassUpdateStatusVo updateStatusVo,BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return ResultVOBuilder.error("500",bindingResult.getFieldError().getDefaultMessage());
        }
        return spareSubService.updateStatus(updateStatusVo);
    }







}
